package com.example.demo.scheduledtask;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.MiscellaneousPostVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class MiscellaneousPost {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;



    /**
     * 其他(杂项)出库过账
     */
//    @Scheduled(cron = "0/30 * * * * ?")//开启定时同步
    @Scheduled(fixedDelay = 30 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void MiscellaneousPost() {
        //查询没有回调的数据
        MiscellaneousPostVo warehouseTransferPost = wmsPurchaseMapper.getMiscellaneousPost();
//        WarehouseTransferPostVo warehouseTransferPost = new WarehouseTransferPostVo();
        if (ObjectUtils.isEmpty(warehouseTransferPost) || ObjectUtil.isEmpty(warehouseTransferPost.getInbadocno())) {
            log.info("未查询到其他(杂项)出库过账数据，修改报错数据重新过账");
            wmsPurchaseMapper.MiscellaneousPostLose();
            return;
        }
        try {
//                        //超过20秒未获取到锁就退出
//            if (!lock.tryLock(20, TimeUnit.SECONDS)) {
//                return;
//            }
            warehouseTransferPost.setInbasite(Constant.PMDSSITE_AMA);
            String resMessage = soapXmlUtil.getSoapXml("MiscellaneousPost", "inba_t", "", warehouseTransferPost, null);
            //转换为soap消息

            log.info("soap其他(杂项)出入库过账请求报文->{}", resMessage);

//            log.info(resMessage);
            //状态占用
            wmsPurchaseMapper.updateMiscellaneousPost(warehouseTransferPost.getInbadocno(), "erp待返回", 3);
            //发送soap消息，并解析结果
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            log.info("soap其他(杂项)出入库过账回调返回报文->{}", result);
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            if (xmlResp.getCode().equals("0")) {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateMiscellaneousPost(warehouseTransferPost.getInbadocno(), xmlResp.getData().get("inbadocno"), 1);
            } else {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateMiscellaneousPost(warehouseTransferPost.getInbadocno(), xmlResp.getMessage(), 2);
            }
        } catch (Exception e) {
            wmsPurchaseMapper.updateMiscellaneousPost(warehouseTransferPost.getInbadocno(), "非常见的数据处理问题，请联系开发人员处理", 2);
            e.printStackTrace();
        }
//        finally {
//            log.error("finally释放锁{}", this.getClass().getName());
//            if (ObjectUtil.isNotEmpty(lock) && lock.isHeldByCurrentThread()) {
//                lock.unlock();
//            }
//        }

    }
}
